package com.greenant.store.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.activity.OrderDetailActivity;
import com.greenant.store.bean.Order;
import com.greenant.store.bean.OrderItem;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

public class MyEarningListAdapter extends BaseAdapter {

	private List<Order> list;
	private Context context;
	// BitmapUtils bitmapUtils;
	private String[] orderStatus;
	private final static String TAG = "MyOrderListAdapter";

	String cash_on_delivery = "";
	DecimalFormat df = new DecimalFormat("#########.##");

	public MyEarningListAdapter(List<Order> tlist, Context ctx) {
		this.list = tlist;
		this.context = ctx;
		orderStatus = ctx.getResources().getStringArray(R.array.order_status_code);
		cash_on_delivery = context.getString(R.string.order_ensure_pay_method_cash_on_delivery);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.item_earning, null);
			viewHolder = new ViewHolder();

			viewHolder.orderno = (TextView) convertView.findViewById(R.id.orderno);
			viewHolder.receive = (TextView) convertView.findViewById(R.id.receive);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			viewHolder.income = (TextView) convertView.findViewById(R.id.income);
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Order order = list.get(position);

		StoreInfo tempStore = AntHomeApplication.storeInfos.get(order.getStoreId());
		List<OrderItem> orderItems = order.getOrderItems();
		if (orderItems.size() > 0) {
			String name = "";
			String imageUrl = "";
			for (int i = 0; i < orderItems.size(); i++) {
				String temp = orderItems.get(i).getName();
				if (!TextUtils.isEmpty(temp)) {
					name += (temp + "+");
				}
				String url = orderItems.get(i).getPicUrl();
				if (TextUtils.isEmpty(imageUrl)) {
					imageUrl = url;
				}

			}
			if (name.length() > 0)
				name = name.substring(0, name.length() - 1);
			if (TextUtils.isEmpty(imageUrl)) {
				viewHolder.icon.setImageResource(R.drawable.pictures_no);
			} else {
				ImageLoader.getInstance().displayImage(imageUrl, viewHolder.icon,
						AntHomeApplication.imageDisplayOptions);
			}

		} else {
			viewHolder.icon.setImageResource(R.drawable.pictures_no);
		}

		double totalCost = UtilTools.getTotalCostByOrderItems(orderItems);
		String totalDesc = "￥--.--";
		String totalGoodsDesc = "￥--.--";
		if (totalCost > 0.0001) {
			totalGoodsDesc = "￥" + String.valueOf(totalCost);
			totalCost += order.getDeliveryCost();
			totalDesc = "￥" + String.valueOf(totalCost);
		} else {

		}
		// 收入
		viewHolder.income.setText(totalDesc);
		// 订单号
		viewHolder.orderno.setText(context.getString(R.string.orderdetail_number) + order.getOrderId());
		// 收件人
		viewHolder.receive.setText(context.getString(R.string.earning_recv_name) + order.getConsignee());
		Date date = order.getUpdatedAt();
		// 时间
		viewHolder.time.setText(DateFormat.format("yyyy-MM-dd HH:mm:ss", date));
		
		// 跳转到订单详情页面
        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                Const.order=order;
                context.startActivity(intent);
            }
        });
        
		return convertView;
	}

	class ViewHolder {
		public TextView orderno;
		public TextView receive;
		public ImageView icon;
		public TextView time;
		public TextView income;
	}

}
